<?php
/* 
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */

function getCategory($parent_id=0) {
    global $DB;
    $sql= "SELECT * FROM article_category WHERE parent_id=".intval($parent_id);
    $rs = $DB->query($sql);
    return $rs;
}

function getNewestArticleByCategoryId($categoryId) {
    global $DB;
    $sql= "SELECT id,title,link,short_description,image FROM article WHERE cat_id=".intval($categoryId)." AND status='yes' ORDER BY create_date DESC";
    $rs = $DB->query_first($sql);
    return $rs;
}

function highestView($limit=15) {
    global $DB;
    $sql= "SELECT title,link FROM article WHERE status='yes' ORDER BY view_count DESC limit $limit";
    $rs = $DB->query($sql);
    $totalRows = $DB->num_rows($rs);
    return array(
    "rs" => $rs,
    "totalRows" => $totalRows
    );
}

function get_cat_id_to_arr($parent_id = 0) {
    global $DB;
    if(!$trees) $trees = array();
    $rsCat = $DB->query("SELECT * FROM article_category WHERE  parent_id = ".intval($parent_id));
    if($rsCat) {
        while($rs = $DB->fetch_array($rsCat)) {
            $trees[] = $rs['id'];
        }
    }
    return $trees;
}

function getLastestArticlesByCategoryId($categoryId) {
    global $DB;
    $arrCatId = get_cat_id_to_arr($categoryId);
    $str_cat_id = '';
    for($i=0;$i<count($arrCatId);$i++) {
        if($i==(count($arrCatId)-1)) {
            $str_cat_id .= $arrCatId[$i];
        }else {
            $str_cat_id .= $arrCatId[$i].',';
        }
    }
    if($str_cat_id=='') {
        $sql= "SELECT a.title,a.image,a.link,a.short_description,c.name as cat_name, c.link as cat_link FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE a.cat_id=".intval($categoryId)." AND a.status='yes' ORDER BY a.create_date DESC";
    }else {
        $sql= "SELECT a.title,a.image,a.link,a.short_description,c.name as cat_name, c.link as cat_link FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE a.cat_id=".intval($categoryId)." OR a.cat_id IN ($str_cat_id) AND a.status='yes' ORDER BY a.create_date DESC";
    }

    $rs = $DB->query_first($sql);
    return $rs;
}

function getArticleByCategoryIdOffsetLimit($categoryId,$offset=0,$limit=10) {
    global $DB;
    $arrCatId = get_cat_id_to_arr($categoryId);
    $str_cat_id = '';
    for($i=0;$i<count($arrCatId);$i++) {
        if($i==(count($arrCatId)-1)) {
            $str_cat_id .= $arrCatId[$i];
        }else {
            $str_cat_id .= $arrCatId[$i].',';
        }
    }
    if($str_cat_id == '') {
        $sql= "SELECT id,title,link FROM article WHERE cat_id=".intval($categoryId)." AND status='yes' ORDER BY create_date DESC LIMIT $offset,$limit";
    }else {
        $sql= "SELECT id,title,link FROM article WHERE cat_id=".intval($categoryId)." OR cat_id IN ($str_cat_id) AND status='yes' ORDER BY create_date DESC LIMIT $offset,$limit";
    }

    $rs = $DB->query($sql);
    return $rs;
}

function getLastestArticleFrontPage() {
    global $DB;
    $sql= "SELECT title,image,short_description,link FROM article WHERE front_page='yes' AND status='yes' ORDER BY front_page_date DESC LIMIT 1";
    $rs = $DB->query_first($sql);
    return $rs;
}

function getArticleFrontPage() {
    global $DB;
    $sql= "SELECT title,link FROM article WHERE front_page='yes' AND status='yes' ORDER BY front_page_date DESC LIMIT 1,10";
    $rs = $DB->query($sql);
    $totalRows = $DB->num_rows($rs);
    return array(
    "rs" => $rs,
    "totalRows" => $totalRows
    );
}

function hotProjects($cat_id = 8,$limit = 4) {
    global $DB;
    $arrCatId = get_cat_id_to_arr($cat_id);
    $str_cat_id = '';
    for($i=0;$i<count($arrCatId);$i++) {
        if($i==(count($arrCatId)-1)) {
            $str_cat_id .= $arrCatId[$i];
        }else {
            $str_cat_id .= $arrCatId[$i].',';
        }
    }
    if($str_cat_id!='') {
        $sql = "SELECT title,image,link FROM article WHERE cat_id IN ($str_cat_id) AND status='yes' ORDER BY front_page_date DESC LIMIT $limit";
        $rs = $DB->query($sql);
        return $rs;
    }
}

function getGoldPrice() {
    global $DB;
    $sql = "SELECT * FROM foreign_exchange WHERE type='gold' ORDER BY id";
    $rs = $DB->query($sql);
    return $rs;
}
function getExchange() {
    global $DB;
    $sql = "SELECT * FROM foreign_exchange WHERE type='exchange' ORDER BY id";
    $rs = $DB->query($sql);
    return $rs;
}

?>
